<template>
  <div class="machine-container">
    <el-card>
      <div slot="header" class="clearfix">
        <span>设备管理</span>
        <el-button style="float: right; padding: 3px 0" type="text" @click="handleAdd">添加设备</el-button>
      </div>

      <el-form :inline="true" :model="queryParams" class="search-form">
        <el-form-item label="设备名称">
          <el-input v-model="queryParams.name" placeholder="请输入设备名称" clearable></el-input>
        </el-form-item>
        <el-form-item label="设备编号">
          <el-input v-model="queryParams.code" placeholder="请输入设备编号" clearable></el-input>
        </el-form-item>
        <el-form-item label="设备状态">
          <el-select v-model="queryParams.status" placeholder="请选择设备状态" clearable>
            <el-option label="全部" value=""></el-option>
            <el-option label="正常" value="normal"></el-option>
            <el-option label="故障" value="fault"></el-option>
            <el-option label="维护中" value="maintenance"></el-option>
            <el-option label="离线" value="offline"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">查询</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="machineList" border style="width: 100%" v-loading="loading">
        <el-table-column prop="id" label="ID" width="80">
        </el-table-column>
        <el-table-column prop="name" label="设备名称">
        </el-table-column>
        <el-table-column prop="code" label="设备编号" width="180">
        </el-table-column>
        <el-table-column prop="location" label="设备位置">
        </el-table-column>
        <el-table-column prop="status" label="设备状态" width="120">
          <template slot-scope="scope">
            <el-tag :type="scope.row.status | statusFilter">{{ scope.row.status | statusTextFilter }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="lastMaintenanceTime" label="最后维护时间" width="180">
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180">
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-container">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :current-page="queryParams.pageNum" :page-sizes="[10, 20, 30, 50]" :page-size="queryParams.pageSize"
          layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
      </div>
    </el-card>

    <!-- 添加/编辑设备对话框 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="50%">
      <el-form :model="machineForm" :rules="rules" ref="machineForm" label-width="100px">
        <el-form-item label="设备名称" prop="name">
          <el-input v-model="machineForm.name" placeholder="请输入设备名称"></el-input>
        </el-form-item>
        <el-form-item label="设备编号" prop="code">
          <el-input v-model="machineForm.code" placeholder="请输入设备编号"></el-input>
        </el-form-item>
        <el-form-item label="设备位置" prop="location">
          <el-input v-model="machineForm.location" placeholder="请输入设备位置"></el-input>
        </el-form-item>
        <el-form-item label="设备状态" prop="status">
          <el-select v-model="machineForm.status" placeholder="请选择设备状态">
            <el-option label="正常" value="normal"></el-option>
            <el-option label="故障" value="fault"></el-option>
            <el-option label="维护中" value="maintenance"></el-option>
            <el-option label="离线" value="offline"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备描述" prop="description">
          <el-input type="textarea" v-model="machineForm.description" placeholder="请输入设备描述"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getMachineList, addMachine, updateMachine, deleteMachine } from '@/api/machine'

export default {
  name: 'Machine',
  filters: {
    statusFilter (status) {
      const statusMap = {
        'normal': 'success',
        'fault': 'danger',
        'maintenance': 'warning',
        'offline': 'info'
      }
      return statusMap[status] || ''
    },
    statusTextFilter (status) {
      const statusTextMap = {
        'normal': '正常',
        'fault': '故障',
        'maintenance': '维护中',
        'offline': '离线'
      }
      return statusTextMap[status] || ''
    }
  },
  data () {
    return {
      queryParams: {
        name: '',
        code: '',
        status: '',
        pageNum: 1,
        pageSize: 10
      },
      machineList: [],
      total: 0,
      loading: false,
      dialogVisible: false,
      dialogTitle: '',
      machineForm: {
        id: '',
        name: '',
        code: '',
        location: '',
        status: 'normal',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入设备名称', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入设备编号', trigger: 'blur' }
        ],
        location: [
          { required: true, message: '请输入设备位置', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择设备状态', trigger: 'change' }
        ]
      }
    }
  },
  created () {
    this.getList()
  },
  methods: {
    getList () {
      this.loading = true
      getMachineList(this.queryParams).then(response => {
        this.machineList = response.data.list
        this.total = response.data.total
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    handleQuery () {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery () {
      this.queryParams = {
        name: '',
        code: '',
        status: '',
        pageNum: 1,
        pageSize: 10
      }
      this.getList()
    },
    handleSizeChange (val) {
      this.queryParams.pageSize = val
      this.getList()
    },
    handleCurrentChange (val) {
      this.queryParams.pageNum = val
      this.getList()
    },
    handleAdd () {
      this.dialogTitle = '添加设备'
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.machineForm.resetFields()
        this.machineForm = {
          id: '',
          name: '',
          code: '',
          location: '',
          status: 'normal',
          description: ''
        }
      })
    },
    handleEdit (index, row) {
      this.dialogTitle = '编辑设备'
      this.dialogVisible = true
      this.$nextTick(() => {
        this.machineForm = Object.assign({}, row)
      })
    },
    handleDelete (index, row) {
      this.$confirm('确认删除该设备吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteMachine(row.id).then(response => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    submitForm () {
      this.$refs.machineForm.validate(valid => {
        if (valid) {
          if (this.machineForm.id) {
            updateMachine(this.machineForm).then(response => {
              this.$message({
                type: 'success',
                message: '修改成功!'
              })
              this.dialogVisible = false
              this.getList()
            })
          } else {
            addMachine(this.machineForm).then(response => {
              this.$message({
                type: 'success',
                message: '添加成功!'
              })
              this.dialogVisible = false
              this.getList()
            })
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.machine-container {
  padding: 20px;

  .search-form {
    margin-bottom: 20px;
  }

  .pagination-container {
    margin-top: 20px;
    text-align: right;
  }
}
</style>